package com.woniu.job.controller;

import com.woniu.job.entity.Company;
import com.woniu.job.entity.Resume;
import com.woniu.job.entity.Work;
import com.woniu.job.service.ICompanyService;
import com.woniu.job.service.IResumeService;
import com.woniu.job.service.IWorkService;
import com.woniu.job.util.ResponseEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;

/**
 * 首页展示（controller类）
 */

@Controller
@RequestMapping("")
public class IndexController {
    private final static Logger logger = LoggerFactory.getLogger(FontIndexController.class);

    @Autowired
    private IWorkService workService;
    @Autowired
    private IResumeService resumeService;
    @Autowired
    private ICompanyService companyService;

    /**
     * 首页显示职位信息
     *
     * @param model
     * @return
     */
    @GetMapping("")
    public String index(Model model) {
        List<Work> works = workService.getWorksMap();
        List<Resume> resumes = resumeService.getResumes();
        List<Resume> resumest = resumeService.getResumes();

        logger.debug(works.toString());
        logger.debug(resumes.toString());


        model.addAttribute("resumes", resumes);
        model.addAttribute("works", works);
        model.addAttribute("resumest", resumest);
        return "index";
    }

    /**
     * 首页退出
     *
     * @return：返回到登录页面
     */
    @GetMapping("logout")
    public String logout(HttpServletRequest request) {
        Enumeration em = request.getSession().getAttributeNames();
        while(em.hasMoreElements()){
            request.getSession().removeAttribute(em.nextElement().toString());
        }

        return "login";
    }

    /**
     * 根据最新时间排列招聘最新职位
     *
     * @return
     */
    @GetMapping("gxtime")
    @ResponseBody
    public ResponseEntity getWorkByTime() {
        List<Work> works = workService.getWorkByTime();
        return new ResponseEntity(200, true, "成功", works);
    }

    /**
     * 根据最新时间排列招聘最新职位
     *
     * @return
     */
    @GetMapping("resume")
    @ResponseBody
    public ResponseEntity getWorkBycategory() {
        List<Work> works = workService.getWorksMap();
        return new ResponseEntity(200, true, "成功", works);
    }

    /**
     * 更多职位
     *
     * @return：职位的列表页
     */
    @GetMapping("list")
    @ResponseBody
    public ResponseEntity joblist(Model model) {
        List<Work> works = workService.getWorksMap();
        return new ResponseEntity(200, true, "成功", works);
    }

    @GetMapping("job_list")
    public String job() {
        return "job_list";
    }


    /**
     * 职位的详情
     *
     * @return：职位的详情页
     */
    @GetMapping("jobs/{id}")
    public String view(@PathVariable String id, Model model) {
        Work work = workService.getCompWorkById(id);
        logger.debug("职位信息：{}", work);
        model.addAttribute("work", work);
        return "comp_jobs";
    }

    /**
     * 职位的详情
     *
     * @return：职位的详情页
     */
    @GetMapping("job_view/{id}")
    public String jobview(@PathVariable String id, Model model) {
        Work work = workService.getCompWorkById(id);
        model.addAttribute("work", work);
        return "job_view";
    }


    /**
     * 更多简历
     *
     * @return：简历的列表页
     */
    @GetMapping("resume_list")
    public String resumelist(Model model) {
        List<Resume> resumes = resumeService.getResumes();
        List<Resume> resumest = resumeService.getResumes();

        model.addAttribute("resumes", resumes);
        model.addAttribute("resumest", resumest);
        return "resume_list";
    }

    /**
     * 简历
     *
     * @return：简历的详情页
     */
    @GetMapping("resume_view/{id}")
    public String resumeviewById(@PathVariable String id, Model model) {
        Resume resume = resumeService.getResumeById(id);
        model.addAttribute("resume", resume);
        return "resume_view";
    }

    /**
     * @return：企业的详情页
     */
    @GetMapping("comp_view/{id}")
    public String resumeview(@PathVariable String id, Model model) {
        logger.debug(id);
        Company company = companyService.getCompanysById(id);
        List<Work> works = workService.getWorksById(id);
        logger.debug("企业信息：{}", company);
        model.addAttribute("company", company);
        model.addAttribute("works", works);
        return "comp_view";
    }

    /**
     * 发布招聘
     *
     * @return：发布招聘详情页
     */
    @GetMapping("company/job_add")
    public String jobadd() {
        return "company/job_add";
    }

    /**
     * 填写简历
     *
     * @return：填写简历列表页
     */
    @GetMapping("personal/resume_1")
    public String resumeadd() {
        return "personal/resume_1";
    }

    @GetMapping("category/{name}")
    @ResponseBody
    public ResponseEntity getWorkList(@PathVariable String name) {
        List<Work> works = workService.getWorksByName(name);
        return new ResponseEntity(200, true, "成功", works);
    }


}
